# -*- coding: utf-8 -*-
# @Author: liuzhen
# @Date:   2018-07-27 11:07:54
# @Last Modified by:   liuzhen
# @Last Modified time: 2018-07-27 11:13:41
import sys
from pymu import MuSymbol
from rv import rvtypes, commands
from rv import qtutils
from rv import runtime
# from rv import io
from PySide.QtGui import *
from PySide.QtCore import *
import subprocess


class PyHello(rvtypes.MinorMode):
    "A simple example that shows how to make shift-Z start/stop playback"

    def togglePlayback(self, event):
        if commands.isPlaying():
            commands.stop()
            print "stopped"
        else:
            commands.play()
            print "playing"
    def export_x264(self, event):
        event.reject()
        # destination = commands.saveFileDialog(True)
        mu_code = """
        {
            [string] arglist;
            string[] args;
            let cmd = system.getenv("RV_APP_RVIO");
            args.push_back(makeTempSession());
            args.push_back(%s);
            args.push_back(%s);
            args.push_back(%s);
            args.push_back(saveFileDialog(true));
            // for_index (i; args) argList = args[args.size()-i-1] : argList;
            // ExternalQProcess(%s, "%s", args, 1, ExternalProcess.Type.ReadOnly, removeTempSession());
        }
        """
        # print mu_code
        runtime.eval(mu_code % ('"-codec"', '"libx264"', '"-o"' ), ["commands", "extra_commands", "rvtypes", "rvui"])

    # def cleanup(self):
    #     C = MuSymbol("export_utils.removeTempSession")
    #     C()
    def test(self,event):
        path = '/opt/rv-Linux-x86-64-6.2.7/bin/rvio'
        # print sys.path
        # print 'hello!ssssssssss'
        # F = MuSymbol("export_utils.makeTempSession")
        # F()
        filename = commands.sessionFileName()
        # print filename
        destination = commands.saveFileDialog(True)
        # command = '/opt/rv-Linux-x86-64-6.2.7/bin/rvio '+filename+' '+'-codec libx264 -o '+destination+'.mov'
        # print command
        arglist = [filename, '-codec', 'libx264', '-o', destination+'.mov']
        # subprocess.call(command,shell=True)
        # p = MuSymbol("external_qprocess.ExternalQProcess")

        # io = MuSymbol("external_qprocess.ExternalQProcess.readyRead")
        # C = MuSymbol("export_utils.removeTempSession")
        # Tp = MuSymbol("rvtypes.ExternalProcess.Type.ReadOnly")
        # D = MuSymbol("external_qprocess.ExternalQProcess.ExternalQProcess")

        # print '-----------------------------------------------------------------------'
        # print dir(D)
        # print '-----------------------------------------------------------------------'
        # D(p, 'test', '/home/liuzhen/Downloads/local_rv/rv-Linux-x86-64-6.2.7/bin/rvio_hw', arglist, 60, Tp, C)
        # C = MuSymbol("export_utils.removeTempSession")

        # C()
        print 'Done!!'

        # print definition

    def xxx(self, event):
        F = MuSymbol("export_utils.makeTempSession")
        F()
        filename = commands.sessionFileName()
        mucode = '''
        {
            let cmd = system.getenv("RV_APP_RVIO");
            string[] args;
            [string] arglist;
            args.push_back("%s");
            args.push_back("%s");
            args.push_back("%s");
            args.push_back("%s");
            args.push_back(saveFileDialog(true));
            print (args.)
            // ExternalQProcess(rvtypes.ExternalProcess;"asd", cmd, args, 1, ExternalProcess.Type.ReadOnly, removeTempSession());
        }
        '''
        runtime.eval(mucode % (filename, '-codec', 'libx264', '-o'), ["commands", "extra_commands", "rvtypes", "rvui"])


    def export_x264_withshell(self, event):
        import time
        start = time.time()
        F = MuSymbol("export_utils.makeTempSession")
        F()
        filename = commands.sessionFileName()
        op_path = commands.saveFileDialog(True)
        op_file = op_path+'.mov'
        command = '/opt/rv-Linux-x86-64-6.2.7/bin/rvio '+filename+' '+'-codec libx264 -o '+op_file
        subprocess.call(command,shell=True)
        end = time.time()
        print '-----------------------------'
        print 'file export to %s..' % (op_file)
        print 'Done!'
        print 'Use '+str(end-start)+'s...'


    def __init__(self):
        rvtypes.MinorMode.__init__(self)
        self.init("pyhello", 
                  None,
                  None,
                  menu=[("File",
                            [("Export",
                                [("Export_X264", self.export_x264_withshell, "Ctrl+Shift+E", None)])])
                       ])

def createMode():
    "Required to initialize the module. RV will call this function to create your mode."
    return PyHello()
